<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            height: 100vh;
            display: flex;

        }

        canvas {
            margin: auto;
            border: 1px solid #d9d9d9;
        }
    </style>
</head>

<body>
    <canvas id="canvas" width="800" height="600"></canvas>
    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');

        // 创建渐变色大小和方向
        const gradient = ctx.createRadialGradient(canvas.width / 2, 0, 10, canvas.width / 2, canvas.height, 100);
        // 设置渐变色值
        gradient.addColorStop(0, 'blue')
        gradient.addColorStop(0.25, 'green')
        gradient.addColorStop(0.5, 'purple')
        gradient.addColorStop(0.75, 'red')
        gradient.addColorStop(1, 'yellow')
        // 将渐变色设置给样式
        ctx.fillStyle = gradient


        // 填充矩形
        ctx.fillRect(0, 0, canvas.width, canvas.height)
    </script>
</body>

</html>